<template>

<!--  <div class="login-container">-->
<!--    <body background="6.jpeg"-->
<!--          style="background-repeat: no-repeat ;-->
<!--          background-attachment:fixed;-->
<!--          backgroud-size:100%,100%">-->
<!--    </body>-->
<!--    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">-->

<!--      <div class="title-container">-->
<!--        <h3 class="title">通用权限管理系统</h3>-->
<!--      </div>-->

<!--      <el-form-item prop="username">-->
<!--        <span class="svg-container">-->
<!--          <svg-icon icon-class="user" />-->
<!--        </span>-->
<!--        <el-input-->
<!--          ref="username"-->
<!--          v-model="loginForm.username"-->
<!--          placeholder="请输入用户名"-->
<!--          name="username"-->
<!--          type="text"-->
<!--          tabindex="1"-->
<!--          autocomplete="on"-->
<!--        />-->
<!--      </el-form-item>-->

<!--      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>-->
<!--        <el-form-item prop="password">-->
<!--          <span class="svg-container">-->
<!--            <svg-icon icon-class="password" />-->
<!--          </span>-->
<!--          <el-input-->
<!--            :key="passwordType"-->
<!--            ref="password"-->
<!--            v-model="loginForm.password"-->
<!--            :type="passwordType"-->
<!--            placeholder="请输入密码"-->
<!--            name="password"-->
<!--            tabindex="2"-->
<!--            autocomplete="on"-->
<!--            @keyup.native="checkCapslock"-->
<!--            @blur="capsTooltip = false"-->
<!--            @keyup.enter.native="handleLogin"-->
<!--          />-->
<!--          <span class="show-pwd" @click="showPwd">-->
<!--            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />-->
<!--          </span>-->
<!--        </el-form-item>-->
<!--      </el-tooltip>-->

<!--      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>-->


<!--    </el-form>-->
<!--  </div>-->





  <div class="site-wrapper site-page--login">
    <div class="site-content__wrapper">
      <div class="site-content">
        <div class="brand-info">
          <h2 class="brand-info__text">工程管理系统</h2>
          <p class="brand-info__intro">版权归黄杰华所有</p>
          <p class="brand-info__intro">协助开发:李亚标，路定川</p>
          <p class="brand-info__intro">版本号:AKJHD001</p>
          <p class="brand-info__intro">备案号:粤ICP备2021071505号-1</p>
        </div>







        <div class="login-main">
          <h3 class="login-title">管理员登录</h3>
          <el-form :model="loginForm"  ref="loginForm" :rules="loginRules"  @keyup.enter.native="handleLogin()" status-icon>
            <el-form-item prop="username">
              <el-input type="text" ref="username" autocomplete="on" tabindex="1" name="username" v-model="loginForm.username" placeholder="帐号"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input :type="passwordType"  ref="password" autocomplete="on" tabindex="2" name="password" v-model="loginForm.password" type="password" placeholder="密码"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button class="login-btn-submit" type="success" @click="regist()">注册</el-button>
            </el-form-item>
            <el-form-item>
              <el-button class="login-btn-submit" type="primary" @click="handleLogin()">登录</el-button>
            </el-form-item>
          </el-form>
        </div>


      </div>

      <el-drawer
        title="会员注册"
        :visible.sync="drawer"
        :direction="direction"
        :with-header="false">
        <span>
          <h1 style="margin-top: 150px" class="login-title">会员注册</h1>
          <el-form ref="form" :model="user" label-width="120px">
            <el-form-item label="用户名">
              <el-input style="width: 300px" v-model="user.username"></el-input>
            </el-form-item>
            <el-form-item label="昵称">
              <el-input style="width: 300px" v-model="user.realName"></el-input>
            </el-form-item>

            <!-- 用户头像 -->
<!--              <el-form-item label="头像">-->
<!--                <el-upload-->
<!--                  :show-file-list="false"-->
<!--                  :on-success="handleAvatarSuccess"-->
<!--                  :before-upload="beforeAvatarUpload"-->
<!--                  class="avatar-uploader"-->
<!--                  :data="uploadHeader"-->
<!--                  action="http://127.0.0.1:9999/api/oss/file/upload?module=avatar"-->
<!--                >-->
<!--                  <img v-if="user.avatar" :src="user.avatar">-->
<!--                  <i v-else class="el-icon-plus avatar-uploader-icon"/>-->
<!--                </el-upload>-->
<!--              </el-form-item>-->

            <el-form-item label="密码">
              <el-input type="password" style="width: 300px" v-model="user.password"></el-input>
            </el-form-item>
            <el-form-item label="电话">
              <el-input style="width: 300px" v-model="user.phone"></el-input>
              <div v-if="show == 0" @click="getcode">
                <el-button style="margin-top: 20px" @click="sendCode(user.phone)">获取验证码</el-button>
              </div>
              <div v-if="show == 1">
                <el-button style="margin-top: 20px">已发送{{count}}秒</el-button>
              </div>
              <div v-if="show == 2" @click="getcode">
                <el-button style="margin-top: 20px" @click="sendCode(user.phone)">重新获取</el-button>
              </div>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input style="width: 300px" v-model="user.email"></el-input>
            </el-form-item>
            <el-form-item label="验证码">
              <el-input style="width: 150px" v-model="user.code"></el-input>
            </el-form-item>
            <el-form-item prop="gender" label="性别">
                <el-radio-group v-model="user.gender">
                  <el-radio :label="0">男</el-radio>
                  <el-radio :label="1">女</el-radio>
                </el-radio-group>
              </el-form-item>

            <div style="text-align: center">
              <el-button type="success" @click="add(user)">确认注册</el-button>
            </div>
          </el-form>
        </span>
      </el-drawer>
    </div>
  </div>
</template>

<script>


import phoneSend from "@/api/phone"
import { getToken } from '@/utils/auth'
import * as THREE from 'three'
import BIRDS from 'vanta/dist/vanta.birds.min'
export default {
  name: 'Login',

  data() {
    return {
      dialogImageUrl:'',
      dialogVisible:false,
      show: 0,
      count: '',
      timer: null,
      //需要验证的验证码
      tokenCode: '',
      //上传需要携带的数据
      // uploadHeader: { "token": getToken() },
      user: {
        username: '',
        password: '',
        avatar:'',
        phone: '',
        realName: '',
        gender: '',
        code: '',
        email: ''
      },
      direction: '',
      drawer: false,
      loginForm: {
        username: '',
        password: ''
      },
      //表单验证规则
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: "请输入用户名" }],
        password: [{ required: true, trigger: 'blur', message: "请输入密码" }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {

    //动态动画
    this.vantaEffect = BIRDS({
      el: this.$refs.vantaRef
    })

    // 如果用户名为空则焦点到用户名输入框
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
      //如果密码为空则焦点到密码输入框
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  beforeDestroy() {
    if (this.vantaEffect) {
      this.vantaEffect.destroy()
    }
  },
  methods: {
    /**
     * * 上传成功回调
     * * @param res *
     * @param file
     * */
    handleAvatarSuccess(res, file) {
      this.user.avatar = res.data
      // 强制重新渲染
      this.$forceUpdate()
    },
    /*** 上传校验 * @param file * @returns {boolean} */
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
      getcode(){
        const times = 60; // 倒计时时间
        if (!this.timer) {
          this.count = times;
          this.show = 1;
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count <= times) {
              this.count--;
            } else {
              this.show = 2;
              clearInterval(this.timer);
              this.timer = null;
            }
          }, 1000)
        }
      },
      async add(user){
        if (this.tokenCode==this.user.code){
          let res = await phoneSend.Register(user)
          if (res.success){
            this.$message.success(res.message);
            this.drawer=false
          }else {
            this.$message.error(res.message);
          }
        }else {
          this.$message.error("验证码错误");
        }

      },
      async sendCode(phone){
        //封装条件
        let params = { phone: phone }
        console.log(phone)
        let res = await phoneSend.send(params)
        if (res.success){
          this.$message.success(res.message);
          console.log(res.data)
          this.tokenCode=res.data
        }else
          this.$message.error(res.message);

      },
      regist(){
          this.direction= 'ltr',
          this.drawer=true
      },
      checkCapslock(e) {
        const { key } = e
        this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
      },
      /**
       * 显示密码
       */
      showPwd() {
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
      /**
       * 处理登录请求
       */
      handleLogin() {
        //进行表单验证
        this.$refs.loginForm.validate(valid => {
          //如果表单验证通过
          if (valid) {
            this.loading = true   //显示加载动画
            //发送登录请求（调用Vuex中的user/login方法）
            this.$store.dispatch('user/login', this.loginForm)
              .then(() => {
                this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
                this.loading = false
              })
              .catch(() => {
                this.loading = false
              })
          } else {
            return false
          }
        })
      },
      getOtherQuery(query) {
        return Object.keys(query).reduce((acc, cur) => {
          if (cur !== 'redirect') {
            acc[cur] = query[cur]
          }
          return acc
        }, {})
      }
    }

}

</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
//用户头像
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9 !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar-uploader img {
  width: 100px;
  height: 100px;
  display: block;
}
.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
//$bg: #283443;
//$dark_gray:#889aa4;
//$light_gray:#eee;
//
//.login-container {
//  min-height: 100%;
//  width: 100%;
//  background-color: $bg;
//  //background-image: url("img.jpeg");
//  background-attachment: fixed;
//  background-size: 100%,100%;
//  overflow: hidden;
//
//  .login-form {
//    position: relative;
//    width: 520px;
//    max-width: 100%;
//    padding: 160px 35px 0;
//    margin: 0 auto;
//    overflow: hidden;
//  }
//
//  .tips {
//    font-size: 14px;
//    color: #fff;
//    margin-bottom: 10px;
//
//    span {
//      &:first-of-type {
//        margin-right: 16px;
//      }
//    }
//  }
//
//  .svg-container {
//    padding: 6px 5px 6px 15px;
//    color: $dark_gray;
//    vertical-align: middle;
//    width: 30px;
//    display: inline-block;
//  }
//
//  .title-container {
//    position: relative;
//
//    .title {
//      font-size: 26px;
//      color: $light_gray;
//      margin: 0px auto 40px auto;
//      text-align: center;
//      font-weight: bold;
//    }
//  }
//
//  .show-pwd {
//    position: absolute;
//    right: 10px;
//    top: 7px;
//    font-size: 16px;
//    color: $dark_gray;
//    cursor: pointer;
//    user-select: none;
//  }
//
//  .thirdparty-button {
//    position: absolute;
//    right: 0;
//    bottom: 6px;
//  }
//
//  @media only screen and (max-width: 470px) {
//    .thirdparty-button {
//      display: none;
//    }
//  }

  .site-wrapper.site-page--login {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: rgba(38, 50, 56, .6);
    overflow: hidden;
    &:before {
      position: fixed;
      top: 0;
      left: 0;
      z-index: -1;
      width: 100%;
      height: 100%;
      content: "";
      background-image: url(~@/assets/11.jpeg);
      background-size: cover;
    }
    .site-content__wrapper {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      padding: 0;
      margin: 0;
      overflow-x: hidden;
      overflow-y: auto;
      background-color: transparent;
    }
    .site-content {
      min-height: 100%;
      padding: 30px 500px 30px 30px;
    }
    .brand-info {
      margin: 220px 100px 0 90px;
      color: #fff;
    }
    .brand-info__text {
      margin:  0 0 22px 0;
      font-size: 48px;
      font-weight: 400;
      text-transform : uppercase;
    }
    .brand-info__intro {
      margin: 10px 0;
      font-size: 16px;
      line-height: 1.58;
      opacity: .6;
    }
    .login-main {
      position: absolute;
      top: 0;
      right: 0;
      padding: 150px 60px 180px;
      width: 470px;
      min-height: 100%;
      background-color: #fff;
    }
    .login-title {
      font-size: 16px;
      text-align: center;
      margin-top: 20px;
      font-family:"新宋体";
    }
    .login-captcha {
      overflow: hidden;
      > img {
        width: 100%;
        cursor: pointer;
      }
    }
    .login-btn-submit {
      width: 100%;
      margin-top: 1px;
    }

}
</style>
